package com.zixuan.sina.controller;


import com.zixuan.sina.entity.vo.RoleVo;
import com.zixuan.sina.service.RoleService;
import com.zixuan.sina.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/role/")
public class RoleController {
    @Resource
    RoleService roleService;
    @GetMapping("getRoleList")
    public Result getRoleList(@RequestParam("roleName") String roleName){
        return roleService.getRoleList(roleName);
    }

    /**
     * 根据角色id查出当前用户所有已经授权的权限
     */

    @GetMapping("getPermissionListByRoleId")
    public Result getPermissionListByRoleId(@RequestParam("roleId") String roleId){
      List<Long>  permissionIds =roleService.getPermissionListByRoleId(roleId);
        return Result.ok(permissionIds);
    }

    @PostMapping("addPermission")
    public Result addPermission(@RequestParam("roleId")String roleId,@RequestBody RoleVo permissionIds){
      int row=  roleService.addPermission(roleId,permissionIds.getChecked());

        return Result.ok(row>0);
    }

    @GetMapping("getRoleById")
    public Result getRoleById(@RequestParam("userId") Long userId){
       Long roleId= roleService.getRoleById(userId);
       return Result.ok(roleId);
    }

    @GetMapping("assignRoles")
    public Result assign(@RequestParam("userId")Long userId,@RequestParam("roleId")Long roleId){
      int row=  roleService.assign(userId,roleId);
        return Result.ok(row);
    }

}
